package com.tencent.sr.iris.activity.service.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.tencent.sr.iris.activity.common.enums.TaskEstimateStatusEnum;
import com.tencent.sr.iris.activity.common.enums.UserTaskStatusEnum;
import com.tencent.sr.iris.activity.interfaces.dto.AwardInfoDTO;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityIdEnum;
import com.tencent.sr.iris.activity.interfaces.enums.IsParticipatedEnum;
import com.tencent.sr.iris.activity.interfaces.request.acticity.BatchDataRequest;
import com.tencent.sr.iris.activity.repository.singlemapper.example.TIrisUserTaskBatchDataExample;
import com.tencent.sr.rmall.material.interfaces.enums.EYesNo;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class UserTaskBatchDataUtils {

    /**
     * 构建条件
     *
     * @param builder
     * @param queryCriteria
     * @param uidList
     */
    public static void buildCondition(BoolQueryBuilder builder, BatchDataRequest queryCriteria, List<String> uidList) {
        String activityType = queryCriteria.getActivityType().getCode();

        builder.must(
                        QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.ACTIVITY_TYPE), activityType))
                .must(QueryBuilders
                        .termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.BATCH_NO), StrUtil.EMPTY))
                .must(QueryBuilders
                        .termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.PARTICIPABLE), EYesNo.YES.name()));

        ActivityIdEnum activityIdEnum = queryCriteria.getActivityId();
        if (activityIdEnum != null) {
            builder.must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.ACTIVITY_ID),
                    activityIdEnum.getCode()));
        }

        Long startTime = queryCriteria.getStartTime();
        Long endTime = queryCriteria.getEndTime();
        if (startTime != null && endTime != null) {
            Date start = DateUtil.beginOfMonth(DateUtil.date(startTime));
            Date end = DateUtil.endOfMonth(DateUtil.date(endTime));
            builder.must(QueryBuilders.rangeQuery(
                            StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.CURRENT_TASK_START_TIME)).gte(start.getTime())
                    .lte(end.getTime()));
        }

        //pid
        String pid = queryCriteria.getPid();
        if (StrUtil.isNotBlank(pid)) {
            builder.must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.PID), pid));
        }

        //uid
        String uid = queryCriteria.getUid();
        if (StrUtil.isNotBlank(uid)) {
            builder.must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.UID), uid));
        }

        //taskIdList
        List<String> taskIdList = queryCriteria.getTaskIdList();
        if (CollectionUtil.isNotEmpty(taskIdList)) {
            builder.must(
                    QueryBuilders.termsQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.TASK_ID), taskIdList));
        }

        //uidList
        if (CollectionUtil.isNotEmpty(uidList)) {
            builder.must(QueryBuilders.termsQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.UID), uidList));
        }

        //区域权限
        List<Long> areaAuths = queryCriteria.getAreaAuths();
        if (CollectionUtil.isNotEmpty(areaAuths)) {
            builder.must(
                    QueryBuilders.termsQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.AREA_ID), areaAuths));
        }

        //下级区域
        List<Long> areaIds = queryCriteria.getAreaIds();
        if (CollectionUtil.isNotEmpty(areaIds)) {
            builder.must(QueryBuilders.termsQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.AREA_ID), areaIds));
        }

        //上级区域
        List<Long> superiorAreaIds = queryCriteria.getSuperiorAreaIds();
        if (CollectionUtil.isNotEmpty(superiorAreaIds)) {
            builder.must(QueryBuilders
                    .termsQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.SUPERIOR_AREA_ID), superiorAreaIds));
        }

        //导师uid
        String advisorUid = queryCriteria.getAdvisorUid();
        if (StrUtil.isNotBlank(advisorUid)) {
            builder.must(QueryBuilders
                    .termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.ADVISOR_UID), advisorUid));
        }

        //上级是否特邀
        Integer pidSpecialInvite = queryCriteria.getPidSpecialInvite();
        if (pidSpecialInvite != null) {
            builder.must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.PID_SPECIAL_INVITE),
                    pidSpecialInvite));
        }

        //下级是否特邀
        Integer uidSpecialInvite = queryCriteria.getUidSpecialInvite();
        if (uidSpecialInvite != null) {
            builder.must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.UID_SPECIAL_INVITE),
                    uidSpecialInvite));
        }

        //下级是否特邀
        List<IsParticipatedEnum> isParticipatedList = queryCriteria.getIsParticipatedList();
        if (CollectionUtil.isNotEmpty(isParticipatedList)) {
            List<String> list = isParticipatedList.stream().map(Enum::name).collect(Collectors.toList());
            builder.must(QueryBuilders.termsQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.IS_PARTICIPATED), list));
        }

        //审核中
        Boolean auditing = queryCriteria.getAuditing();
        if (auditing != null) {
            BoolQueryBuilder auditingBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(
                            StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.IS_PARTICIPATED), IsParticipatedEnum.LIMITED.name()))
                    .must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.USER_TASK_STATUS),
                            UserTaskStatusEnum.PROGRESS.getCode()))
                    .must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.ESTIMATE_USER_TASK_STATUS),
                            TaskEstimateStatusEnum.FINISHED.getCode()));
            if (auditing) {
                builder.must(auditingBuilder);
            } else {
                builder.mustNot(auditingBuilder);
            }
        }
    }

    public static void buildCondition(BoolQueryBuilder builder, BatchDataRequest queryCriteria) {
        buildCondition(builder, queryCriteria, null);
    }

    /**
     * 计算佣金和预估佣金
     *
     * @param commissionMap
     * @return
     */
    public static long getCommission(Map<String, Long> commissionMap) {
        long result = 0L;
        for (Map.Entry<String, Long> record : commissionMap.entrySet()) {
            String taskId = record.getKey();
            Long val = commissionMap.getOrDefault(taskId, 0L);
            result += val;
        }
        return result;
    }
}
